/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package poproject.GUI;

import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.table.DefaultTableModel;
import poproject.OptimizationTask.MapModel;

/**
 * Panel do konfiguracji kosztów "przeorania ziemi" :)
 * @author Eruanno
 */
public class CostPlanerPanel extends javax.swing.JPanel {

    private DefaultTableModel mModel;       //< Model tablicy boisk znajdującej się w GUI
    private MainWindow mMainWindow;         //< Zmienna reprezentująca główne okno programu
    private PTConfigWindow mConfigWindow;   //< Zmienna reprezentująca okno na którym jest wyświetlany panel

    private SpinnerNumberModel mStartPointModel;  //< Model dla kontrolki punktu startowego
    private SpinnerNumberModel mFinishPointModel; //< Model dla kontrolki punktu końcowego
    
    /**
     * Creates new form CostPlanerPanel
     */
    public CostPlanerPanel(MainWindow mainWindow, PTConfigWindow configWindow) {
        initComponents();
        
        mMainWindow = mainWindow;
        mConfigWindow = configWindow;
        mModel = (DefaultTableModel)jTableFields.getModel();
        
        mStartPointModel = (SpinnerNumberModel)jStartPoint.getModel();
        mFinishPointModel = (SpinnerNumberModel)jFinishPoint.getModel();
        
        adjustTableRowCount((int)jLength.getValue());
    }
    
    /**
     * Makes sure that table row count is equal to defined map length
     * @param forcedRowCount 
     */
    private void adjustTableRowCount(int forcedRowCount)
    {
        // Add missing rows if any
        while(mModel.getRowCount() < forcedRowCount)
        {
            mModel.addRow(new Object[] { 0, 0 });
        }
        
        // Remove extra rows if any
        while(mModel.getRowCount() > forcedRowCount)
        {
            mModel.removeRow(mModel.getRowCount()-1);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jNumberedTable2 = new poproject.Common.JNumberedTable();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTableFields = new poproject.Common.JNumberedTable();
        jLabel2 = new javax.swing.JLabel();
        jLength = new javax.swing.JSpinner();
        jHeight = new javax.swing.JSpinner();
        jBtnAccept = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jStartPoint = new javax.swing.JSpinner();
        jFinishPoint = new javax.swing.JSpinner();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        jNumberedTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jNumberedTable2);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Koszt pasków ziemi");

        jTableFields.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Numer", "Koszt"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(jTableFields);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Wymiary pola");

        jLength.setModel(new javax.swing.SpinnerNumberModel(3, 3, 20, 1));
        jLength.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jLengthStateChanged(evt);
            }
        });

        jHeight.setModel(new javax.swing.SpinnerNumberModel(3, 3, 20, 1));

        jBtnAccept.setText("Zatwierdź");
        jBtnAccept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnAcceptActionPerformed(evt);
            }
        });

        jLabel3.setText("Poziom startowy");

        jLabel4.setText("Poziom końcowy");

        jStartPoint.setModel(new javax.swing.SpinnerNumberModel(2, 1, 3, 1));
        jStartPoint.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jStartPointStateChanged(evt);
            }
        });

        jFinishPoint.setModel(new javax.swing.SpinnerNumberModel(3, 1, 3, 1));
        jFinishPoint.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jFinishPointStateChanged(evt);
            }
        });

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("W");

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("H");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel2)
                        .addComponent(jBtnAccept, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLength, javax.swing.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE)
                                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jHeight)
                                .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jStartPoint, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFinishPoint, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLength, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jStartPoint, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFinishPoint, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 128, Short.MAX_VALUE)
                        .addComponent(jBtnAccept)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jLengthStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jLengthStateChanged
        int val = (int) jLength.getValue();
        this.adjustTableRowCount(val-1);

        mStartPointModel.setMaximum(val);
        mFinishPointModel.setMaximum(val);

        if ((int) mStartPointModel.getValue() > val) {
            mStartPointModel.setValue(val);
        }
        if ((int) mFinishPointModel.getValue() > val) {
            mFinishPointModel.setValue(val);
        }
    }//GEN-LAST:event_jLengthStateChanged

    private void jStartPointStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jStartPointStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_jStartPointStateChanged

    private void jFinishPointStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jFinishPointStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_jFinishPointStateChanged

    private void jBtnAcceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnAcceptActionPerformed
        MapModel model;
        
        int areas = (int)jLength.getValue()-1;
        int levels = (int)jHeight.getValue();
        int start = (int)jStartPoint.getValue();
        int stop = (int)jFinishPoint.getValue();
        
        model = new MapModel(areas, levels);
        model.setStartLevel(start);
        model.setFinishLevel(stop);
        
        DefaultTableModel dtm = (DefaultTableModel)jTableFields.getModel();
        
        for(int row = 0; row < dtm.getRowCount(); row++)
        {
            int cost = (int)dtm.getValueAt(row, 1);
            model.setAreaCost(row, cost);
        }
        
        this.mMainWindow.setMapModel(model);
    }//GEN-LAST:event_jBtnAcceptActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnAccept;
    private javax.swing.JSpinner jFinishPoint;
    private javax.swing.JSpinner jHeight;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JSpinner jLength;
    private poproject.Common.JNumberedTable jNumberedTable2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSpinner jStartPoint;
    private poproject.Common.JNumberedTable jTableFields;
    // End of variables declaration//GEN-END:variables
}
